package com.maikang.exammall.api;

import com.maikang.exammall.entity.dto.JsApiTicketDTO;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;

/**
 * jsapi 演示接口的 controller.
 */
@AllArgsConstructor
@RestController
@RequestMapping("/wx")
@Log4j2
public class WxJsapiController {
    private final WxMpService wxService;

    @GetMapping("/js-ticket")
    public JsApiTicketDTO getJsapiTicket(@RequestParam @NotBlank String uri) throws WxErrorException {
        log.info("access token is: " + wxService.getAccessToken());
        log.info("uri is {}", uri);

        final WxJsapiSignature jsapiSignature = this.wxService.switchoverTo(wxService.getWxMpConfigStorage().getAppId()).createJsapiSignature(uri);
        log.info("jsapiSignature: {}", jsapiSignature);
        return new JsApiTicketDTO(this.wxService.getJsapiTicket(true), jsapiSignature);
    }
}
